import pygame
from pygame.locals import *
import string

from gui import *
from events import *
from utils import load_png
import preferences


#------------------------------------------------------------------------------
class MenuGUIView(GUIView):
	"""..."""
	def __init__( self, evManager, renderGroup, rect, game ):
		GUIView.__init__( self, evManager, renderGroup, rect, game)
		
		quitEvent1 = QuitEvent()
		helpEvent = GUIChangeScreenRequest( 'help' )
		signupEvent = GUIChangeScreenRequest( 'signup' )# GameStartRequest()
                #testeDados = GUIDadosDialogAddRequest( 'dadosDialog', [6,4,1], 'ataque' )
                #testeWaiting = GUIWaitingDialogAddRequest( 'waitingDialog' )
		
		quitEvent = GUIDialogAddRequest('msgDialog', 'Deseja mesmo sair do jogo?',
                                                event=quitEvent1,
                                                fechar='Cancelar')

		b1= ButtonSprite( evManager, "Sair", container=self, 
		                  onClickEvent=quitEvent, fontSize=22 )
		b2= ButtonSprite( evManager, "Ajuda", container=self,
		                  onClickEvent=helpEvent )
		b3= ButtonSprite( evManager, "Novo Jogo", container=self,
		                  onClickEvent=signupEvent )
                #b4 = ButtonSprite( evManager, "Jogar Dados", container=self,
		#                  onClickEvent=testeDados )
                #b5 = ButtonSprite( evManager, "Esperar", container=self,
		#                  onClickEvent=testeWaiting )

		
                self.widgets = [ b3,b2,b1 ]

		self.renderGroup.add( self.widgets )

		try:
			if pygame.mixer.music.get_busy():
				pygame.mixer.music.load('data/trooper.mp3')
				pygame.mixer.music.play(-1, 0.0)
				musicPlaying = True
		except pygame.error:
			# se o mixer nao estiver inicializado, suprimimos o problema
			pass
			
		
		self.ArrangeWidgets()

	#----------------------------------------------------------------------
 	def GetBackgroundBlit(self):
		bgImg = pygame.Surface( (self.rect.width, self.rect.height) )
		bgImg.fill( (0,0,50) )
		return [bgImg, self.rect]

	#----------------------------------------------------------------------
 	def Notify(self, event):
		GUIView.Notify( self, event )

		if isinstance( event, GameStartedEvent):
			ev = GUIChangeScreenRequest( 'main' )
			self.evManager.Post( ev )

#------------------------------------------------------------------------------
class SignupGUIView(GUIView):
	"""..."""
	def __init__( self, evManager, renderGroup, rect, game ):
		GUIView.__init__( self, evManager, renderGroup, rect, game)

		self.xyOffset = ( self.rect.x, self.rect.y )

		menuEvent = GUIChangeScreenRequest( 'menu' )
		playEvent = GamePrepareRequest()
		b1= ButtonSprite( evManager, "Menu", container=self,
		                  onClickEvent=menuEvent)
		b2= ButtonSprite( evManager, "Iniciar", container=self,
		                  onClickEvent=playEvent)
	
		playerName = PlayerNameSprite(evManager, self.game.GetCurrentPlayer(),  container=self)
		
		playerColor = LabelSprite( evManager, "Cor", container=self, fontSize=25, fontColor=(255,255,255), fontFamily='data/wareagle.ttf')
		
		self.widgets = [ b1, playerName, playerColor, b2 ]
		
		for c in preferences.gameColors.values():
			self.widgets.append( PlayerColorSprite(evManager, container=self, buttonColor=c) )

		self.renderGroup.add( self.widgets )

		self.ArrangeWidgets()

	#----------------------------------------------------------------------
 	def GetBackgroundBlit(self):
		bgImg = pygame.Surface( (self.rect.width, self.rect.height) )
		bgImg.fill( (50,0,0) )
		return [bgImg, self.rect]

	def ArrangeWidgets(self):
		xyOffset = ( self.rect.x, self.rect.y )
		self.widgets[0].rect.topleft = vectorSum( xyOffset, (20,50) )
		self.widgets[1].rect.topleft = vectorSum( xyOffset, (20,150) )
		self.widgets[2].rect.topleft = vectorSum( xyOffset, (20,205) )
		self.widgets[3].rect.topleft = vectorSum( xyOffset, (20,300) )
		x = 131
		for w in self.widgets[4:]:
			w.rect.topleft = vectorSum( xyOffset, (x,200) )
			x += 33

	def Notify(self, event):
		GUIView.Notify( self, event )

		if isinstance( event, GameStartedEvent):
			ev = GUIChangeScreenRequest( 'main' )
			self.evManager.Post( ev )

#------------------------------------------------------------------------------
class PlayerNameSprite(TextEntrySprite):
	def __init__(self, evManager, name, container=None):
		TextEntrySprite.__init__( self, evManager, 'Nome', fontSize=25, fontColor=(255,255,255), fontFamily='data/wareagle.ttf', container=self, distance=33)
		self.widgets[1] = PlayerNameTextBoxSprite( evManager, 
		                                           200, 
							   name = name,
		                                           container=container )

#------------------------------------------------------------------------------
class PlayerNameTextBoxSprite(TextBoxSprite):
	def __init__(self,evManager,width, name, container=None):
		TextBoxSprite.__init__(self,evManager,width,container=container)
		TextBoxSprite.SetText(self,name)
	
	def SetText(self, newText):
		self.text = newText
		self.evManager.Post(SetCurrentPlayerNameEvent(newText))
		self.dirty = 1

#------------------------------------------------------------------------------
class PlayerColorSprite(ColorEntrySprite):
	def __init__(self, evManager, container=None, buttonColor=(0,0,0)):
		ColorEntrySprite.__init__( self, evManager, '', buttonColor, fontSize=0, container=self)
		self.widgets[1] = PlayerColorButtonSprite( evManager, buttonColor, container=container )

#------------------------------------------------------------------------------
class PlayerColorButtonSprite(ButtonColorSprite):
	def __init__(self,evManager,buttonColor,container=None):
		ButtonColorSprite.__init__(self,evManager,buttonColor,container=container)
		self.color = buttonColor
	
	def SetPlayerColor(self):
		c = self.color
		color = [k for k, v in preferences.gameColors.iteritems() if v == c][0]
		self.evManager.Post(SetCurrentPlayerColorEvent(color))
		self.dirty = 1

#------------------------------------------------------------------------------
class HelpGUIView(GUIView):
	"""..."""
	def __init__( self, evManager, renderGroup, rect, game ):
		GUIView.__init__( self, evManager, renderGroup, rect, game)

		self.xyOffset = ( self.rect.x, self.rect.y )

		menuEvent = GUIChangeScreenRequest( 'menu' )
		b1= ButtonSprite( evManager, "Menu", container=self,
		                  onClickEvent=menuEvent)
		
		self.widgets = [ b1 ]

		for l in open('data/help.txt'):
			self.widgets.append( LabelSprite( evManager, unicode(l, "utf-8").rstrip(), container=self, fontColor=(255,255,255), fontSize=20 ) )

		self.renderGroup.add( self.widgets )

		self.ArrangeWidgets()

	#----------------------------------------------------------------------
 	def GetBackgroundBlit(self):
		bgImg = pygame.Surface( (self.rect.width, self.rect.height) )
		bgImg.fill( (50,0,0) )
		return [bgImg, self.rect]

	def ArrangeWidgets(self):
		xyOffset = ( self.rect.x, self.rect.y )
		y = 100
		self.widgets[0].rect.topleft = vectorSum( xyOffset, (20,50) )
		for w in self.widgets[1:]:
			w.rect.topleft = vectorSum( xyOffset, (20,y) )
			y += 20

if __name__ == "__main__":
	print "that was unexpected"
